var UserID;
Ext.define('AM.view.window.Login', {
    extend: 'Ext.window.Window',
    alias : 'widget.login',
    title : 'Login',
    id:'login',
    layout: 'fit',
    autoShow: true,
    modal : true,
        
    initComponent: function() {
    var me = this;
        this.items = [
            {
                xtype: 'form',
                bodyPadding: '5px,5px,5px,5px',
                layout: 'anchor',
                width: 400,
                defaults: {
                    anchor: '100%'
                },
                items: [{
                    xtype: 'displayfield',
                    id: 'if',
                    hidden : true,
                    fieldLabel: 'Info'
                },{
                    xtype: 'displayfield',
                    id: 'er',
                    hidden : true,
                    fieldLabel: 'Eroare'
                },{
                    xtype: 'textfield',
                    name : 'email',
                    id:'email',
                    value:'test@gmail.com',
                    fieldLabel: 'E-mail',
                    vtype: 'email',
                    allowBlank: false
                },{
                    xtype: 'textfield',
                    name : 'pass',
                    id:'pass',
                    value:'test',
                    inputType: 'password',
                    fieldLabel: 'Password',
                    allowBlank: false
                },{
                    xtype: 'textfield',
                    name : 'rpass',
                    id:'rpass',
                    value:'test',
                    inputType: 'password',
                    fieldLabel: 'Repeta Password',
                    allowBlank: false,
                    hidden : true,
                    
                },{
                	xtype: 'displayfield',
                	id:'inreg',
                	value: '<a href="#"> Creaza cont </a>',
                	listeners:{
                		afterrender:function(){
                			this.el.select("a").on("click", function(){
                				Ext.getCmp('rpass').show();
                				me.resetValue();
                				Ext.getCmp('inreg').hide();
                				Ext.getCmp('nor').show();
                				Ext.getCmp('loginn').hide();
                				Ext.getCmp('regis').show();
                			}, this);
                		}
                	}
                },{
                	xtype: 'displayfield',
                	id:'nor',
                	value: '<a href="#"> Logare normala </a>',
                	hidden : true,
                	listeners:{
                		afterrender:function(){
                			this.el.select("a").on("click", function(){
                				Ext.getCmp('rpass').hide();
                				Ext.getCmp('pass').setValue('');
                				Ext.getCmp('inreg').show();
                				Ext.getCmp('nor').hide();
                				Ext.getCmp('loginn').show();
                				Ext.getCmp('regis').hide();
                			}, this);
                		}
                	}
                }
                ]
            }
        ];

        this.buttons = [
            {
                text: '<b>Login</b>',
                id:'loginn',
                //action: 'loginn',
                handler:this.Login,
            	scope: this
            },{
                text: '<b>Inregistrare</b>',
                id:'regis',
                handler: this.Inregistrare,
            	scope: this,
            	hidden : true,
            },{
                text: 'Cancel',
                scope: this,
                handler: this.close
            }
        ];

        this.callParent(arguments);
    },
    Login: function (obj,e) {
        var me = this;
    	Ext.getCmp('er').hide();
        	Ext.Ajax.request({
            clientValidation: true,
            url: 'data/login.php',
            scope: this,
            params: {
            	acction:'login',
            	email: Ext.getCmp('email').getValue(),
                pass: hex_md5(Ext.getCmp('pass').getValue())
            },
            success: function(response, opts) {
                var obj = Ext.decode(response.responseText);
                if(obj.success == false){
                	Ext.getCmp('er').setValue('<b><span style="color:red;">' + obj.rows[0].task + '</span></b>').show();
                }else{
                	UserID = obj.rows[0].id;
                	Ext.getCmp('userlist').getStore().load({
                  	    params: {storeTip: 'grid', acction: 'dateout', period: 'year', UserID: UserID},
                  		callback : function() {
                  			Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                  			//Ext.getStore('StoreComboCild').load({params: { acction :'combocild',UserID:UserID}});
                  			Ext.getStore('StoreComboPro').load({params: {storeTip: 'comboPro', acction: 'combopro', UserID: UserID}});
                  			//Ext.getStore('StoreComboSubpro').load({params: { acction :'combosubpro',UserID:UserID}});
                  			Ext.getStore('StoreComboTip').load({params: {storeTip: 'comboTip', acction: 'combotip', UserID: UserID}});
                  			Ext.getStore('StoreDetail').load({params: {storeTip: 'detail', acction: 'dey', UserID: UserID}});
                  			Ext.getStore('StoreDetailSum').load({params: {storeTip: 'details_sum', acction: 'day', UserID: UserID}});
                  			Ext.getStore('StoreGrafic').load({params: {storeTip: 'graphic', acction: 'grafic', UserID: UserID}});
                            me.showAll();
                            Ext.Ajax.request({
                                url: 'data/tools.php',
                                scope: this,
                                method: "GET",
                                params: {
                                    storeTip: 'getSumInit',
                                    acction: 'getSumInit',
                                    UserID: UserID
                                    },
                                success: function(response){
                                    var obj = Ext.decode(response.responseText);
                                    var responseData = obj.rows[0];
                                    Ext.getCmp('sumInit').setValue(responseData.sumInit);
                                }
                            });
                  		}
                    });
                }
            },
            failure: function(response, opts) {
                console.log('server-side failure with status code ');
            }
        });	    
	},
	Inregistrare: function (obj,e) {
		console.log('Inregistrare');
		var me = this;
    	Ext.getCmp('er').hide();
    	Ext.Ajax.request({
//            clientValidation: true,
            url: 'data/login.php',
            scope: this,
            params: {
            	acction:'regis',
            	email: Ext.getCmp('email').getValue(),
                pass: hex_md5(Ext.getCmp('pass').getValue()),
                rpass: hex_md5(Ext.getCmp('rpass').getValue())
            },
            success: function(response, opts) {
            	var obj = Ext.decode(response.responseText);
                if(obj.success == false){
              	    Ext.getCmp('if').hide();
              	    Ext.getCmp('er').setValue('<b><span style="color:red;">'+obj.rows[0].task+'</span></b>').show();
                    me.resetValue();
                }else{
                	UserID = obj.rows[0].id;
                    me.showAll();
                    Ext.getStore('StoreComboTip').load({params: {storeTip: 'comboTip', acction: 'combotip', UserID: UserID}});
                }
            },
            failure: function(response, opts) {
            	me.resetValue();
            	Ext.getCmp('if').hide();
            	Ext.getCmp('er').setValue('<b><span style="color:red;">' + a.result.rows[0].task + '</span></b>').show();
            }
        });	    
	},

	showAll: function() {
     	    Ext.getCmp('startpanel').hide();
             Ext.getCmp('generalpanel').show();
             Ext.getCmp('meniu').show();
             Ext.getCmp('treepane').show();
             Ext.getCmp('detailpanel').show();
             Ext.getCmp('gr').show();
             Ext.getCmp('login').hide();
    },
    resetValue: function() {
    	    Ext.getCmp('rpass').setValue('');
            Ext.getCmp('email').setValue('');
            Ext.getCmp('pass').setValue('');
    }
});